#include <string.h>
#include <stdlib.h>

#include "psd_auth.h"
#include "psd_auth_anon.h"
#include "psd_modules.h"

int psd_auth_anon_authenticate(psdConn *conn, psdCreds **ret_creds);
int psd_auth_anon_request_authentication (psdSess *sess, psdConn *conn);
const char *psd_auth_anon_get_value(psdCreds *credentials);
void psd_auth_anon_free_credentials(psdCreds *credentials);

static psdAuthenticationHandler psd_auth_anon_handler = {
	.authenticate = psd_auth_anon_authenticate,
	.request_authentication = psd_auth_anon_request_authentication,
	.name = "ANON",
};

static psdModule psd_auth_anon_module = {
	.desc = "Anonymous Authentication Module",
	.dependencies = "",
	.init = psd_auth_anon_init
};

psdModule *module_info() {
	return &psd_auth_anon_module;
}

int psd_auth_anon_init() {
	return psd_add_authentication_handler(&psd_auth_anon_handler);
}

const char *psd_auth_anon_get_value(psdCreds *credentials) {

	return "";
}

void psd_auth_anon_free_credentials(psdCreds *credentials) {
	free(credentials);
}

int psd_auth_anon_request_authentication (psdSess *sess, psdConn *conn) {
	return 0;
}

int psd_auth_anon_authenticate(psdConn *conn, psdCreds **ret_creds) {
	psdCreds *creds;

	creds = malloc(sizeof(psdCreds));
	if (!creds)
		goto error_exit;

	creds->type = "ANON";
	creds->private = NULL;
	creds->get_user = psd_auth_anon_get_value;
	creds->get_email = psd_auth_anon_get_value;
	creds->get_institution = psd_auth_anon_get_value;
	creds->free = psd_auth_anon_free_credentials;


	*ret_creds = creds;

	return 0;

error_exit:
	return -1;
}
